package com.spring.cloud.seata.service;

import com.spring.cloud.seata.api.RemoteAccountService;
import com.spring.cloud.seata.api.RemoteStorageService;
import com.spring.cloud.seata.domain.Order;
import com.spring.cloud.seata.mapper.OrderMapper;
import io.seata.core.context.RootContext;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author zhangtengfei
 * @date 2021/10/18 11:23
 */
@Slf4j
@Service
public class OrderServiceImpl implements IOrderService {

    @Resource
    private RemoteStorageService remoteStorageService;

    @Resource
    private RemoteAccountService remoteAccountService;

    @Resource
    private OrderMapper orderMapper;

    /**
     * 同一个数据库也可以使用分布式事务测试
     * 服务发起者加这个注解
     *
     * @param order
     */
    @GlobalTransactional(name = "prex-create-order", rollbackFor = Exception.class)
    @Override
    public void createOrder(Order order) {
        log.info("当前 XID: {}", RootContext.getXID());
        log.info("下单开始, 用户:{}, 商品:{}, 数量:{}, 金额:{}", order.getUserId(), order.getProductId(), order.getCount(), order.getPayMoney());
        // 创建订单
        order.setStatus(0);
        orderMapper.saveOrder(order);
        log.info("保存订单{}", order);

        //远程调用库存服务扣减库存
        log.info("扣减库存开始");
        remoteStorageService.reduceCount(order.getProductId(), order.getCount());
        log.info("扣减库存结束");

//        //远程调用账户服务扣减余额
//        log.info("扣减余额开始");
//        remoteAccountService.reduceBalance(order.getUserId(), order.getPayMoney());
//        log.info("扣减余额结束");

        System.out.println(1 / 0);

        // 修改订单状态为已完成
        log.info("修改订单状态开始");
        orderMapper.updateOrderStatusById(order.getId(), 1);
        log.info("修改订单状态结束");

        log.info("下单结束");
    }
}

